<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/16
 * Time: 15:10
 */

//数组转字符串，带key
function array2string($array){

    $string = [];

    if($array && is_array($array)){

        foreach ($array as $key=> $value){
            $string[] = $key.'='.$value;
        }
    }

    return implode('&',$string);
}

function dealAliMutiData($data,$arr){
    $arr = json_decode($arr,true);
    switch ($data['trade_status']){
        case 'WAIT_BUYER_PAY':
            $reason='交易创建，等待买家付款';
            break;
        case 'TRADE_CLOSED':
            $reason='未付款交易超时关闭，或支付完成后全额退款';
            break;
        case  'TRADE_SUCCESS':
            $reason='交易支付成功';
            break;
        default:
            $reason='交易结束，不可退款';
            break;
    }
    foreach($arr as $k => $v){
        $arr[$k]['statusReason'] = $reason;
        $arr[$k]['type'] = 0;
        $arr[$k]['amount'] = $v['num'] * $v['price'];
        $arr[$k]['trade_status'] = $data['trade_status'];
        $arr[$k]['trade_no'] = $data['trade_no'];
        $arr[$k]['gmt_payment'] = strtotime($data['gmt_payment']);
        $arr[$k]['buyer_id'] = $data['buyer_id'];
    }

    return $arr;
}

function dealWechatMutiData($data,$arr){
    $arr = json_decode($arr,true);
    switch ($data['result_code']){
        case 'SUCCESS':
            $reason='交易成功';
            break;
        default:
            $reason='交易失败';
            break;
    }

    foreach($arr as $k => $v){
        $arr[$k]['statusReason'] = $reason;
        $arr[$k]['type'] = 1;
        $arr[$k]['create_time']=time();
        $arr[$k]['amount'] = $v['num'] * $v['price'];
        $arr[$k]['trade_status'] = $data['result_code'];
        $arr[$k]['trade_no'] = $data['transaction_id'];
        $arr[$k]['gmt_payment'] = time();
        $arr[$k]['buyer_id'] = $data['openid'];
    }

    return $arr;
}

function dealAliData($data){
    switch ($data['trade_status']){
        case 'WAIT_BUYER_PAY':
            $reason='交易创建，等待买家付款';
            break;
        case 'TRADE_CLOSED':
            $reason='未付款交易超时关闭，或支付完成后全额退款';
            break;
        case  'TRADE_SUCCESS':
            $reason='交易支付成功';
            break;
        default:
            $reason='交易结束，不可退款';
            break;
    }

    $attachArr=explode('_',urldecode($data['passback_params']));
    $data['pid']=$attachArr[1];
    $data['user']=$attachArr[4];
    $data['ptype']=$attachArr[2];
    $data['num']=$attachArr[3];
    $data['memo']=$attachArr[7];
    $data['address']=$attachArr[6];
    $data['phone']=$attachArr[5];
    $data['goodname']=$attachArr[8];
    $data['typename']=$attachArr[9];
    $data['statusReason']=$reason;
    $data['create_time']=time();
    $data['type']=0;
    $data['amount'] = $data['total_amount'];

    return $data;
}

function dealWechatData($data){
    switch ($data['result_code']){
        case 'SUCCESS':
            $reason='交易成功';
            break;
        default:
            $reason='交易失败';
            break;
    }

    $attachArr=explode('_',$data['attach']);
    $data['pid']=$attachArr[1];
    $data['user']=$attachArr[4];
    $data['ptype']=$attachArr[2];
    $data['num']=$attachArr[3];
    $data['memo']=$attachArr[7];
    $data['address']=$attachArr[6];
    $data['phone']=$attachArr[5];
    $data['goodname']=$attachArr[8];
    $data['typename']=$attachArr[9];
    $data['statusReason']=$reason;
    $data['create_time']=time();
    $data['trade_status']=$data['result_code'];
    $data['type']=1;
    $data['trade_no']=$data['transaction_id'];
    $data['gmt_payment']=time();
    $data['amount']=$data['total_fee']/100;
    $data['buyer_id']=$data['openid'];

    return $data;
}

function curl_get($url){
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 超时设置,以秒为单位
    curl_setopt($curl, CURLOPT_TIMEOUT, 1);

    // 超时设置，以毫秒为单位
    // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

    // 设置请求头
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    //执行命令
    $data = curl_exec($curl);

    // 显示错误信息
    if (curl_error($curl)) {
        print "Error: " . curl_error($curl);
    } else {
        // 打印返回的内容
        var_dump($data);
        curl_close($curl);
    }
}

function decryptData($sessionKey,$encryptedData, $iv )
{
    if (strlen($sessionKey) != 24) {
        return 'IllegalAesKey';
    }
    $aesKey=base64_decode($sessionKey);

    if (strlen($iv) != 24) {
        return 'IllegalIv';
    }
    $aesIV=base64_decode($iv);

    $aesCipher=base64_decode($encryptedData);

    $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
    $dataObj=json_decode( $result,true);

    \think\facade\Log::record('info:'.json_encode($dataObj),'info');
    if( $dataObj  == NULL )
    {
        return 'IllegalBuffer';
    }
    if( $dataObj['watermark']['appid'] != 'wxc1c600cb9953ca42' )
    {
        return 'IllegalBuffer';
    }
    return $dataObj;
}